﻿<?php
require_once('../../../config.php');
require_once($CFG->libdir . '/adminlib.php');
require_once($CFG->dirroot . '/local/myplugin/lib.php');
require_once($CFG->dirroot . "/local/myplugin/forms.php");
define('AJAX_SCRIPT', true);
global $DB;

$id = required_param('id', PARAM_TEXT);

/* Ket noi den file javascript - module.js */
$PAGE->requires->js_init_call('M.local_myplugin');	
$mycss =  $CFG->wwwroot.'/local/myplugin/css/mycss.css';
 echo '<link rel="stylesheet" href="'.$mycss.'" type="text/css" media="screen" />';
 
//==================start colorbox=================
echo '<script src="../colorbox/jquery.min.js"></script>';
echo '<link rel="stylesheet" href="../css/colorbox.css" />';
echo '<script src="../colorbox/jquery.colorbox.js"></script>';
//browser detection
echo '<script language="javascript" defer="false">
//browser detection
    var agt=navigator.userAgent.toLowerCase();
    var is_major = parseInt(navigator.appVersion);
    var is_minor = parseFloat(navigator.appVersion);

    var is_nav  = ((agt.indexOf("mozilla")!=-1) && (agt.indexOf("spoofer")==-1)
                && (agt.indexOf("compatible") == -1) && (agt.indexOf("opera")==-1)
                && (agt.indexOf("webtv")==-1) && (agt.indexOf("hotjava")==-1));
    var is_nav4 = (is_nav && (is_major == 4));
    var is_nav6 = (is_nav && (is_major == 5));
    var is_nav6up = (is_nav && (is_major >= 5));
    var is_ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
</script>'; 
 
$cssdir =  $CFG->wwwroot.'/local/myplugin/css/mycss.css';
echo '<link rel="stylesheet" href="'.$cssdir.'" type="text/css" media="screen" />';
echo '<script src="module.js"></script>';

/* Viet doan code cua ban o day */

$mylib = new curriculum_management();

$mform = new form_modify_subject(null, array('id'=>$id));

if ($fromform = $mform->get_data()) 
{	
    //$ts_id_string = $_POST['teachersubjectid'];
    //echo "<script>alert('".$ts_id_string ."');</script>";
    

    
    $ta = $fromform->ta_temp;
   // echo "<script>alert('TA".$ta."');</script>";
    $ins = $fromform->instructor_temp;
    //echo "<script>alert('ins".$ins."');</script>";
    $id_subject = $_POST['id'];
    
     $teachers_array = array();
     $teachers_array = $DB->get_records('teacher_subject', array('subject'=>$id_subject));
     foreach($teachers_array as $teacher_array)
     {
         $DB->delete_records('teacher_subject', array('subject'=>$id_subject));
     }
    
    if($ins != NULL)
    {
    $ins_id = explode(',,', $ins);
    $n = count($ins_id);
    //echo "<script>alert('ins".$n."');</script>";
    $role = 1;
    //echo "<script>alert('".$id_subject."');</script>";
    $mylib->add_teacher_subject($n,$id_subject,$ins_id,$role);
    }
    else
    {
        $role = 1;
        $teachers_array = array();
        $teachers_array = $DB->get_records('teacher_subject', array('subject'=>$id_subject,'role'=>$role));

        foreach($teachers_array as $teacher_array)
        {
        $DB->delete_records('teacher_subject', array('subject'=>$id_subject,'role'=>$role));
        }
    }
    
    if($ta != NULL)
    {
    //echo "<script>alert('TA".$ta."');</script>";
    $ta_id = explode(',,', $ta);
    
    $n2 = count($ta_id);
   // echo "<script>alert('".$n2."');</script>";
    $role= 2;
    $mylib->add_teacher_subject($n2,$id_subject,$ta_id,$role);
    }
    else
    {
        $role = 2;
        $teachers_array = array();
        $teachers_array = $DB->get_records('teacher_subject', array('subject'=>$id_subject,'role'=>$role));

        foreach($teachers_array as $teacher_array)
        {
        $DB->delete_records('teacher_subject', array('subject'=>$id_subject,'role'=>$role));
        }
    }


    $program = $_POST['programid']; 
	$option['id'] = $_POST['id'];
	$option['subject_id']= $fromform->txtid;
	$option['subject_name_vn']= $fromform->txttenvn;
	$option['subject_name_en']= $fromform->txtteneng;
	$option['subject_credit']= $fromform->txttinchi;
	$option['subject_theory_hr']= $fromform->txtlythuyet;
	$option['subject_practice_hr']= $fromform->txtthuchanh;
	$option['abstract'] = $fromform->txtdesc;
	//$program = $_POST['programid'];
	$option['program_id'] = $program;
	$mylib->update_subject($option);
		
	//close colorbox
	echo "<script>parent.$.fn.colorbox.close(); </script>";
	
}


$mform->display();

/*------Ket thuc doan code cua ban-------*/

